package com.onion.utils.http;

import com.onion.utils.utils.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

@Deprecated
public class Test {
    public static void main(String[] args) {
        try {
            int i = 100;
            System.out.println("开始下载："+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            downlodFile("https://insurance-online.oss-cn-beijing.aliyuncs.com/product/crm/upload/call/ZTTH_YBJJ/20221121/20221121142654-7000343770239627264-756676.mp3");
            System.out.println("结束下载："+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

        } catch (Exception e) {
        }
    }

    public static void downlodFile(String urlpath) throws Exception {
        HttpURLConnection conn = null;
        URL url = new URL(urlpath);
        conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(3000);
        InputStream inputStream = FileDownloadLimitUtil.getLimitInputStream(conn.getInputStream(), 100);
        FileOutputStream outputStream = new FileOutputStream("d:/aa2.mp3");
        byte[] bytes = new byte[1024];
        int read;
        while ((read=inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    }

    public static void down2(String url) throws Exception {
        HttpGet request;
        request = new HttpGet(url);
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        InputStream inputStream = FileDownloadLimitUtil.getLimitInputStream(is, -1);
        FileOutputStream outputStream = new FileOutputStream("d:/aa.mp3");
        byte[] bytes = new byte[1024];
        int read;
        while ((read=inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    }

}
